<template>
  <div class="feedback-form">
    <h1>意见反馈</h1>
    <form @submit.prevent="handleSubmit">
      <div class="form-group">
        <label for="name">如何称呼您:</label>
        <input type="text" id="name" v-model="form.name" required />
      </div>
      <div class="form-group">
        <label for="email">Email:</label>
        <input type="email" id="email" v-model="form.email" required />
      </div>
      <div class="form-group">
        <label for="message">您的反馈:</label>
        <textarea id="message" v-model="form.message" required></textarea>
      </div>
      <button type="submit">提交</button>
    </form>
    <div v-if="submitted" class="confirmation">
      <p>感谢您的反馈!</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        name: '',
        email: '',
        message: ''
      },
      submitted: false
    };
  },
  methods: {
    handleSubmit() {
      // 在这里你可以添加提交表单的逻辑，例如发送数据到服务器
      console.log('Form submitted:', this.form);
      this.submitted = true;

      // 发送到后端接口
      fetch('http://127.0.0.1:5000/feedback', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(this.form)
      })
        .then(response => response.json())
        .then(data => {
          if (data.code === 200) {
            console.log('Success:', data.message);
          } else {
            console.error('Error:',data.message);
          }
        })


      // 重置表单
      this.form.name = '';
      this.form.email = '';
      this.form.message = '';
    }
  }
};
</script>

<style scoped>
.feedback-form {
  max-width: 500px;
  margin: 10% auto;
  padding: 20px;
  border: 1px solid #e0e0e0;
  border-radius: 10px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  background-color: #ffffff;
}

h1 {
  font-size: 24px;
  margin-bottom: 20px;
  color: #333;
}

.form-group {
  margin-bottom: 20px;
  text-align: left;
}

label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
  color: #555;
}

input,
textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
  box-sizing: border-box;
  transition: border-color 0.3s;
}

input:focus,
textarea:focus {
  border-color: #007bff;
  outline: none;
}

textarea {
  min-height: 100px;
  resize: vertical;
}

button {
  width: 100%;
  padding: 10px 0;
  background-color: #007bff;
  color: #fff;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #0056b3;
}

.confirmation {
  margin-top: 20px;
  padding: 15px;
  border: 1px solid #c3e6cb;
  background-color: #d4edda;
  border-radius: 5px;
  color: #155724;
}
</style>
